package com.ifcar.app.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.ContentValues;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.ifcar.app.db.NewsSQLiteOpenHelper;
import com.ifcar.app.util.CollectArticleAdapter;
import com.ifcar.app.util.CollectPicturesAdapter;
import com.ifcar.app.util.CollectUtil;
import com.ifcar.app.util.StringUtil;
import com.ifcar.app.util.ThemeSettingsHelper;
import com.ifcar.app.activity.R;


public class CollectDeleteActivity extends BaseActivity {

	private RelativeLayout collect_layout_bg;
	
	private TextView main_text;
	private ListView collectList;
	private GridView collectGridView;
	private RelativeLayout middle_context;
	private CollectPicturesAdapter pgridadapter;
	private CollectArticleAdapter articleDeleteAdapter;
	private CheckBox select_all_checkbox;
	private Button deleteBtn;
	private Button cancelBtn;

	private int type;
	
	private List<Map<String, Object>> deleteData;
	private CollectUtil cu;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.collect_delete);

		type = getIntent().getIntExtra("type", -1);

		findViews();
		updateViewContent();
		
		select_all_checkbox.setOnClickListener(new CheckBox.OnClickListener() {
			@Override
			public void onClick(View v) {
				

				ContentValues initialValues = new ContentValues();
				
				if(type==CollectUtil.COLLECT_PICTURES){
					if(select_all_checkbox.isChecked()){
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "1");
						pgridadapter.setCheckOrNoAllBox(true);
					}else{
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "0");
						pgridadapter.setCheckOrNoAllBox(false);
					}
					db.update(NewsSQLiteOpenHelper.COLLECT_TABLE, initialValues, "collect_type=?", new String[]{String.valueOf(type)});
					pgridadapter.notifyDataSetChanged();
				}else if(type==CollectUtil.COLLECT_ARTICLE ||type==CollectUtil.COLLECT_COMMENT){
					if(select_all_checkbox.isChecked()){
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "1");
						articleDeleteAdapter.setCheckOrNoAllBox(true);
					}else{
						initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "0");
						articleDeleteAdapter.setCheckOrNoAllBox(false);
					}
					
					String sql = "";
					String conditions[] = null;
					if(type==CollectUtil.COLLECT_COMMENT){
						sql = "collect_type=? or collect_type=?";
						conditions = new String[]{String.valueOf(CollectUtil.COLLECT_COMMENT), String.valueOf(CollectUtil.COLLECT_PIC_COMMENT)};
					}else{
						sql = "collect_type=?";
						conditions = new String[]{String.valueOf(CollectUtil.COLLECT_ARTICLE)};
					}
					
					db.update(NewsSQLiteOpenHelper.COLLECT_TABLE, initialValues, sql, conditions);
					articleDeleteAdapter.notifyDataSetChanged();
				}else{
					articleDeleteAdapter.notifyDataSetChanged();
					for(int i = 0; i < articleDeleteAdapter.getCount(); i ++){
						
					}
				}
				
			}
		});
		
		ContentValues initialValues = new ContentValues();
		initialValues.put(NewsSQLiteOpenHelper.COLLECT_ISCHECKED, "0");
		db.update(NewsSQLiteOpenHelper.COLLECT_TABLE, initialValues, null, null);
	
	}
	
	public void goBackClick(View view){
        finish();
    }
	
	private void findViews(){
		collect_layout_bg = (RelativeLayout)findViewById(R.id.collect_layout_bg);
		main_text = (TextView)findViewById(R.id.top_main_text);		
		select_all_checkbox = (CheckBox)findViewById(R.id.select_all_checkbox);
		middle_context = (RelativeLayout)findViewById(R.id.middle_context);
		
		collectList = (ListView)findViewById(R.id.collect_article_list);
		collectGridView = (GridView)findViewById(R.id.collect_pictures_gridview);
		
		deleteBtn = (Button)findViewById(R.id.collect_delete_submit);
		cancelBtn = (Button)findViewById(R.id.collect_delete_cancel);
	}
	
	private void updateViewContent(){
		main_text.setText(getResources().getString(R.string.main_title_collect_delete));
		
		switch (type) {
		case CollectUtil.COLLECT_ARTICLE:
		case CollectUtil.COLLECT_COMMENT:
			collect_layout_bg.setBackgroundResource(R.color.collect_layout_bg);
			middle_context.setVisibility(View.VISIBLE);			
			
			break;
		case CollectUtil.COLLECT_PICTURES:
			collect_layout_bg.setBackgroundResource(R.drawable.pictures_bg);
			middle_context.setVisibility(View.VISIBLE);
			
			break;
		}
		
		setListViewData();
		
		deleteBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				int selectNum;
				
				if(type==CollectUtil.COLLECT_PICTURES){
					selectNum = pgridadapter.getSelectNums();
				}else{
					selectNum = articleDeleteAdapter.getSelectNums();
				}
				
				if(selectNum <= 0){
					Toast.makeText(CollectDeleteActivity.this,
							"没有选中任何选项，不能进行删除操作", Toast.LENGTH_LONG).show();
				}else{
					
					StringBuilder sql = new StringBuilder();
					String conditions[] = null;
					
					if(type == CollectUtil.COLLECT_COMMENT){
						sql.append("(").append(NewsSQLiteOpenHelper.COLLECT_TYPE).append(" =? or ").append(NewsSQLiteOpenHelper.COLLECT_TYPE)
							.append(" =? ) and ").append(NewsSQLiteOpenHelper.COLLECT_ISCHECKED).append("=1");
						
						conditions = new String[]{String.valueOf(CollectUtil.COLLECT_COMMENT), String.valueOf(CollectUtil.COLLECT_PIC_COMMENT)};
						
					}else{
						sql.append(NewsSQLiteOpenHelper.COLLECT_TYPE).append(" =? and ").append(NewsSQLiteOpenHelper.COLLECT_ISCHECKED).append("=1");
						conditions = new String[]{String.valueOf(type)};
					}
					
					int summ = db.delete(NewsSQLiteOpenHelper.COLLECT_TABLE, StringUtil.GetString(sql), conditions);
					
					Toast.makeText(CollectDeleteActivity.this,
							"删除"+summ+"条记录", Toast.LENGTH_LONG).show();
					CollectDeleteActivity.this.finish();
				}
			}
		});
		
		cancelBtn.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				CollectDeleteActivity.this.finish();
			}
		});
		
	}
	
	private void setListViewData(){
		cu = new CollectUtil(CollectDeleteActivity.this);
		deleteData = cu.getCollectObjects(type);
		if(type==CollectUtil.COLLECT_PICTURES){
			List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
			for(Map<String, Object> o:deleteData)
				data.add(o);
			
			collectGridView.setVisibility(View.VISIBLE);
			collectList.setVisibility(View.GONE);
			pgridadapter = new CollectPicturesAdapter(this, data, false,select_all_checkbox);
			collectGridView.setAdapter(pgridadapter);
			
		}else{
			
			collectList.setVisibility(View.VISIBLE);
			collectGridView.setVisibility(View.GONE);
			
			articleDeleteAdapter = new CollectArticleAdapter(CollectDeleteActivity.this, 
					deleteData, false, select_all_checkbox);
			
			collectList.setAdapter(articleDeleteAdapter);
		}
	}
	
	@Override
    public void onApplyTheme() {
        super.onApplyTheme();
        ThemeSettingsHelper themeSettingsHelper = getThemeSettingsHelper();
        themeSettingsHelper.setTextViewColor(this, R.id.top_main_text, R.color.main_title_color);
        themeSettingsHelper.setImageViewSrc(this, R.id.top_id_image, R.drawable.netease_top);
        themeSettingsHelper.setViewBackgroud(this, R.id.top_main_bottom, R.drawable.top_background);
        themeSettingsHelper.setViewBackgroud(this, R.id.collect_title_layout, R.drawable.top_column_bg);
        themeSettingsHelper.setViewBackgroudColor(this, collect_layout_bg, R.color.main_bg_color);
        themeSettingsHelper.setTextViewColor(this, R.id.collect_delete_all_title, R.color.column_item_color);
        themeSettingsHelper.setListViewDivider(this, collectList, R.drawable.list_divider_line);
        themeSettingsHelper.setViewBackgroud(this, deleteBtn, R.drawable.netease_small_button);
        themeSettingsHelper.setViewBackgroud(this, cancelBtn, R.drawable.netease_small_button);
        themeSettingsHelper.setTextViewColor(this, deleteBtn, R.color.list_title_color);
        themeSettingsHelper.setTextViewColor(this, cancelBtn, R.color.list_title_color);
        themeSettingsHelper.setViewBackgroud(this, R.id.collect_delete_layout, R.drawable.top_column_bg);
        themeSettingsHelper.setImageViewSrc(this, R.id.go_back_common_btn, R.drawable.go_back);
        if (articleDeleteAdapter != null) {
            articleDeleteAdapter.notifyDataSetChanged();
        }
        if (pgridadapter != null) {
            pgridadapter.notifyDataSetChanged();
        }
    }
}
